package pab.ui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.swing.*;

import pab.ui.auxiliary.Base64;


/*
 * Created by JFormDesigner on Mon Jun 06 19:53:59 ICT 2011
 */

/**
 * @author rukowen
 */
public class Login extends JDialog {
	/**
	 * 
	 */
	String propPath;
	boolean isLoginOK;
	private static final long serialVersionUID = 1L;
	public Login(String propPath) {
		initComponents();
		this.propPath = propPath;
		isLoginOK = false;
		BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("wallet-icon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
		setModal(true);
	}

	private void btnLoginMouseClicked(MouseEvent e) {

		try {
			Properties prop = new Properties();
			FileInputStream fis;
			fis = new FileInputStream(new File(propPath));
			try {
				prop.load(fis);
				@SuppressWarnings("deprecation")
				String pwd = Base64.encode(passwordField.getText()) ;
				
				if(prop.get("user").equals(pwd)){
					isLoginOK = true;
					this.dispose();
				}
				else{
					isLoginOK = false;
					JOptionPane.showMessageDialog(this, "Invalid password");
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}

	private void passwordFieldKeyPressed(KeyEvent e) {
		// TODO add your code here
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
			btnLoginMouseClicked(null);
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		label1 = new JLabel();
		passwordField = new JPasswordField();
		btnLogin = new JButton();

		//======== this ========
		setTitle("Login");
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label1 ----
		label1.setText("Password");
		contentPane.add(label1);
		label1.setBounds(new Rectangle(new Point(56, 32), label1.getPreferredSize()));

		//---- passwordField ----
		passwordField.setColumns(1);
		passwordField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				passwordFieldKeyPressed(e);
			}
		});
		contentPane.add(passwordField);
		passwordField.setBounds(145, 26, 171, passwordField.getPreferredSize().height);

		//---- btnLogin ----
		btnLogin.setText("Login");
		btnLogin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				btnLoginMouseClicked(e);
			}
		});
		contentPane.add(btnLogin);
		btnLogin.setBounds(new Rectangle(new Point(145, 59), btnLogin.getPreferredSize()));

		contentPane.setPreferredSize(new Dimension(385, 130));
		setSize(385, 130);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JLabel label1;
	private JPasswordField passwordField;
	private JButton btnLogin;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
